const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");

//查询预付预收结存金额
router.post('/bill/getYusfLeft', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    try {
        let buId = body.buId ? body.buId : 0;
        let type = body.type ? body.type : '';

        let sql = '';
        // 预收结余
        if (type === 'yus') {
            sql = `SELECT sum(price2-price1) amount from v_card_detail  a where a.tenantId=${tenantId} 
            and billState='accounted'  and a.subjectCode='2203'  and buId=${buId} `;
        }
        // 预付结余
        if (type === 'yuf') {
            sql = `SELECT sum(price1-price2) amount from v_card_detail  a where a.tenantId=${tenantId} 
            and billState='accounted'  and a.subjectCode='1123'  and buId=${buId} `;
        }
        let jy = 0
        let data = await db.query(sql, { type: QueryTypes.SELECT, plain: true })
        if (!data) {
            jy = data.amount;
        }
        return ctx.suc('', jy);
    } catch (e) {
        return ctx.err(e);
    }

})
module.exports = router;